//table显示ID
var tableDiv="tableSerialnumberVODiv";
//详情对话框
var detailDiv="detailSerialnumberVODiv";
//表单DIV
var formDiv = "addSerialnumberVOForm";
//添加对话框
var addDlgDiv ="addSerialnumberVODiv";
//添加按钮
var addBtn = "SerialnumberVOAddBtn";
//新增按钮  
var addDlgVO = "adddataVO";
//模糊搜索ID
var searchDiv= "searchSerialnumberVOForm";

//对象查询地址
var queryUrl="../system/serialnumberVO/querySerialnumberVOPageList";
//对象增加地址
var addUrl="../system/serialnumberVO/saveSerialnumberVO"
//对象修改地址
var updateUrl="../system/serialnumberVO/updateSerialnumberVOById"
//对象删除地址
var deleteUrl="../system/serialnumberVO/deleteSerialnumberVOById"
//对象详情地址
var detailUrl="../system/serialnumberVO/querySerialnumberVOById"
//图片显示路径
var ftp = "http://172.16.1.52:3001/"
//datatable 列表
var msgTable;

$(function(){
	
	msgTable = $('#tableSerialnumberVODiv').DataTable({
		"bProcessing": true,
		"bLengthChange":true,
		"bServerSide": true,
		"bFilter": false,
		"bSort": false,
		"sAjaxSource": "../system/serialnumberVO/querySerialnumberVOPageList",
	    "fnServerParams" : function(aDataSet) {
	    	aDataSet.push(
	    		{"name" : "serialNumber","value" : $("#searchSerialnumberVOForm #serialNumberSearch").val()},
	    		{"name" : "serialExt1","value" : $("#searchSerialnumberVOForm #serialExt1Search").val()}
	    		);
	    	},
	    "fbServerData":function(sSource,aDataSet){$.ajax({"dataType":"json","type":"GET","url":sSource,"data":aDataSet});},
	    "aoColumns":[{"mDataProp":"serialId"},
	                 {"mDataProp":"serialNumber"},
	                 {"mDataProp":"serialExt1"},
	                 {"mDataProp":"serialExt2"},
	                 {"mDataProp":"serialId"}],	      
	    "aoColumnDefs": [
	                     {
	                    	"aTargets":[4],
	                    	"mRender":function(data,type,full){
	                    		var detailBtn = "<button class='btn btn-white btn-outline m-wrap' data-value='"+data+"' data-id='"+full.serialId+"' id='detailDataBtn' style='font-size:12px;line-height:15px;'><i class='icon-search'></i></button> "  
	                    		var updateBtn = "<button class='btn btn-lightblue btn-outline m-wrap' data-id='"+ full.serialId +"' id='updateDataBtn' style='font-size:12px;line-height:15px;'><i class='icon-pencil'></i></button> "  
	                    		var deleteBtn = "<button class='btn btn-lightpink btn-outline m-wrap' data-id='"+ full.serialId +"' id='deleteDataBtn' style='font-size:12px;line-height:15px;'><i class='icon-remove'></i></button>"
	                    		return detailBtn+updateBtn+deleteBtn;
	                    	}
	                     }
	                     ],      				       
	    "oLanguage":{"sUrl": "../resources/assets/plugins/dataTables/txt/page.txt"}
	 });

	//初始化时间插件
	$(".ui_timepicke").datepicker({
		  language:'zh-CN',
		  CustomFormat: "yyyy-mm-dd",
	      minView: "month", 
	      changeYear: true,
	      yearRange: "-200:+0",
	      timepicker:false,    
	      todayBtn: true,
	      autoclose:true
	});
	
	//显示序列号某行的详情
	$("#"+tableDiv+" tbody").on("click","#detailDataBtn",function(){
		var pk=$(this).attr("data-id");
		$.post(detailUrl,{serialId:pk},function(data){
			showDetailData(data);
		});
	});
	
	//显示详情对话框
	function showDetailData(data){
		for(var k in data){
			var ele =$("#"+detailDiv+" #"+k+"Detail");
			ele.html(data[k]);
		}
		$("#"+detailDiv).modal("show");
	};
	
	//删除
	$("#"+tableDiv+" tbody").on("click","#deleteDataBtn",function(){
		var pk=$(this).attr("data-id");
		confirm("您确定要删除此条信息吗 ?", function(isConfirm){
			if(isConfirm){
				$.post(deleteUrl,{serialId:pk},function(data){
					if(data !=null && typeof(data)!="undefined"){
						if(data.flg==true){
							swal("成功","操作成功","success");
						}else if(data.flg==false){
							swal("失败","操作失败","error");
						}
					}
					reloadData();
				});
				
			}else{
				swal("取消","您的操作已取消","error");
			}
		});
	});
	
	var tabForm = $('#' + formDiv);
    var errorForm = $('.alert-error', tabForm);
    var successForm = $('.alert-success', tabForm);
	//初始化表单-新增和删除
	function initFormValidate(param){
		var url="";
		if(param=="add"){
			url=addUrl;
		}else if(param=="update"){
			url=updateUrl;
		}
		$("#"+formDiv).attr("action",url);
		
		//表单验证(新)
		$("#"+formDiv).validate({
	        errorElement: 'span', //default input error message container
	        errorClass: 'help-inline', // default input error message class
	        focusInvalid: false, // do not focus the last invalid input
	        ignore: "",
	        rules: {
	        		serialNumber:{required:true,}, 
	        		serialExt1:{required:true,}, 
	        		serialExt2:{required:true,}, 
	        		serialExt3:{} 
	        },
	        invalidHandler: function (event, validator) { //display error alert on form submit              
	            successForm.hide();
	            errorForm.show();
	            App.scrollTo(errorForm, -200);
	        },
	        highlight: function (element) { // hightlight error inputs
	            $(element).closest('.help-inline').removeClass('ok'); // display OK icon
	            $(element).closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
	        },
	        unhighlight: function (element) { // revert the change dony by hightlight
	            $(element).closest('.control-group').removeClass('error'); // set error class to the control group
	        },
	        success: function (label) {
	            label.addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
	            label.closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
	        },
	        messages:{teacherName:{pattern:'必须是汉字'}},
	        submitHandler: function (form) {
	            successForm.show();
	            errorForm.hide();
	            valditFlag = true;//防止重复录入
	            //文件上传成功后，向后台插入数据
	            tabForm.ajaxSubmit({
					type:"post",
					url:tabForm.attr("action"),
					success:function(data){
						if(data.flg==true){
							swal("成功","操作成功", "success");
							$("#"+addDlgDiv).modal("hide");
							reloadData();
						}else if(data.flg==false){
							swal("失败","操作失败","error");
							$("#"+addDlgDiv).modal("hide");
							reloadData();
						}
						
					}
				});
	            return false;
	        }
   		});
   		
	}
	
	
	//新增按钮
	$("#"+addDlgVO).bind("click",function(){
		reset();
		$("#"+addBtn).text("保存");
		$("#" + formDiv+ " #"+addBtn).attr("disabled",false);
		valditFlag =false;
		$("#"+ addDlgDiv).modal("show");
		initFormValidate("add");
	});
	
	//新增数据保存按钮
	$("#"+addBtn).on("click",function(){
		if(valditFlag){
			return;
		}
		$('#' + formDiv).submit();
		resetRequired();
		reloadData();
	});
	
	//修改
	$("#"+tableDiv+" tbody").on("click","#updateDataBtn",function(){
		var pk=$(this).attr("data-id");
		$("."+formDiv+"_checktip").html(""); 
		reset();
		$("#"+addBtn).text("修改");
		$.ajax({url:detailUrl,data:{serialId:pk},type:"post",async:false,success:function(data){
			if(data !=null && typeof(data)!="undefined"){
				for(var k in data){
					var ele =$("#"+formDiv+" #"+k);
					ele.val(data[k]);
				}
				valditFlag = false;
				$("#"+ addDlgDiv).modal("show");
				initFormValidate("update");
			}
		}
		});
		initFormValidate("update");
	});
	
	//初始化表单
	function reset(){
		$("#"+formDiv).find("input").val("");
		$(".help-inline").closest('.control-group').removeClass('ok');
   		$(".help-inline").closest('.control-group').removeClass('error');
   		$(".help-inline").closest('.control-group').removeClass('success');
   		$(".help-inline").removeClass('ok');
   	    $(".help-inline").html('');
   	    $("br[name='br']").remove();
   	    $(".alert-success").attr("display","none");
   	    $(".alert-error").attr("display","none");
   	    $(".alert-error").attr("style","");
   	    $(".alert-success").attr("style","");
   	    //$('#' + formDiv).reset();
	}
	
	//修改验证样式
	function resetRequired(){
	}
	
});
